package com.hspedu.newData.dmsxl.动态规划.股票问题;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-27 
 * @Version: 1.0
 */


public class LC122买卖股票的最佳时机Ⅱ {

    // 贪心算法
    public int maxProfit(int[] prices) {
        int res = 0;

        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i-1]){
                res += prices[i] - prices[i-1];
            }
        }

        return res;
    }


    // 动态规划
    public int maxProfit1(int[] prices) {

        int len = prices.length;
        if(len < 2) return 0;


        // dp[i][0]  定义为第i天持有股票的收益
        // dp[i][1]  定义为第i天持有现金的收益

        int[][] dp = new int[len][2];

        dp[0][0] = -prices[0];
        dp[0][1] = 0;

        for (int i = 1; i < len; i++) {
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1] - prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0] + prices[i]);
        }

        return dp[len-1][1];
    }

    @Test
    public void test() {
        // 测试代码
    }
}
